package com.manage.widgets;

import com.manage.R;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class CustomDialog extends Dialog implements android.view.View.OnClickListener {

	private static final String TAG = "CustomDialog";
	
	private Button btnConfirm = null;
	private Button btnCancel = null;
	private TextView tvDialog = null;
	private EditText etFeedback = null;
	
	private String title = "";
	private boolean show = false;
	
    private Window window = null;
    
    DialogActionListener dialogAction;
			
	public CustomDialog(Context context, String paramString, boolean paramBoolean) {
		super(context);
		this.title = paramString;
		this.show = paramBoolean;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.menu_confirm_dialog); 
        
        btnConfirm = (Button)findViewById(R.id.MenuQuitDialogOK);
        btnConfirm.setOnClickListener(this);
        btnCancel = (Button)findViewById(R.id.MenuQuitDialogCancel);
        btnCancel.setOnClickListener(this);
        
        tvDialog = (TextView)findViewById(R.id.tv_dialog);
        tvDialog.setText(this.title);
        
        etFeedback = (EditText)findViewById(R.id.et_feedback);
        int showFeedback = this.show ? View.VISIBLE : View.GONE;
        etFeedback.setVisibility(showFeedback);
        
        //设置触摸对话框意外的地方取消对话框
        setCanceledOnTouchOutside(true);
	}

	@Override
	public void show() {
		super.show();
		windowDeploy(0, 0);
	}

	//设置窗口显示
    private void windowDeploy(int x, int y){
        window = getWindow(); //得到对话框
        window.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
        window.setBackgroundDrawableResource(R.color.transparent); //设置对话框背景为透明
        WindowManager.LayoutParams wl = window.getAttributes();
        //根据x，y坐标设置窗口需要显示的位置
        wl.x = x; //x小于0左移，大于0右移
        wl.y = y; //y小于0上移，大于0下移  
//        wl.alpha = 0.6f; //设置透明度
//        wl.gravity = Gravity.BOTTOM; //设置重力
        window.setAttributes(wl);
    }
    
	@Override
	public void onClick(View v) {
		int viewID = v.getId();
		switch(viewID) {
		case R.id.MenuQuitDialogOK:
			if(etFeedback.getVisibility() == View.VISIBLE) {
				String feedback = etFeedback.getText().toString();
				dialogAction.confirmAction(feedback, null);
			} else {
				dialogAction.confirmAction();
			}
			
			break;
		case R.id.MenuQuitDialogCancel:
			dismiss();
			break;
		default:
			break;
		}
	}
	
	/**
	 * 接口方法，用于点击dialog中”确定“按钮的事件处理
	 * @param listener
	 */
	public void setDialogConfirmAction(DialogActionListener listener) {
		this.dialogAction = listener;
	}
}
